package com.future.lock.find;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.future.lock.R;
import com.future.lock.common.http.HttpUtils;
import com.future.lock.find.adapter.FindAdapter;
import com.future.lock.find.bean.FindListBean;
import com.future.lock.me.entity.response.FindListResponse;

import java.util.ArrayList;

import butterknife.BindColor;
import butterknife.BindInt;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * 发现Fragment
 */
public class FindFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    Unbinder unbinder;
    private FindAdapter adapter;

    private int page = 0;

    public FindFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_find, container, false);
        unbinder = ButterKnife.bind(this, view);

        initView();
        updateData();
        return view;
    }

    private void updateData() {
        HttpUtils.getHttpApi().getInfoList(page++).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                refreshLayout.setRefreshing(false);
                FindListResponse findListResponse = new FindListResponse();
                findListResponse.parse(response.body());
                if (findListResponse.success == 1) {
                    if (page == 1) {
                        adapter.setNewData(findListResponse.data);
                    }else{
                        adapter.addData(findListResponse.data);
                        if (findListResponse.data.size()<2) {
                            adapter.loadMoreEnd();
                        }else {
                            adapter.loadMoreComplete();
                        }
                    }
                }else{
                    Toast.makeText(getContext(), findListResponse.msg, Toast.LENGTH_SHORT).show();
                    adapter.loadMoreFail();
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                refreshLayout.setRefreshing(false);
                Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
                adapter.loadMoreFail();
            }
        });
    }

    private void initView() {
        refreshLayout.setRefreshing(true);
        refreshLayout.setColorSchemeColors(Color.parseColor("#648DDD"));
        refreshLayout.setOnRefreshListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new FindAdapter(null);
        recyclerView.setAdapter(adapter);

        View emptyView = getActivity().getLayoutInflater().inflate(R.layout.layout_default_empty, null);
        adapter.setEmptyView(emptyView);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getContext(),NewsDetailActivity.class);
                intent.putExtra("id", ((FindListBean) adapter.getData().get(position)).id);
                startActivity(intent);
            }
        });

//        adapter.disableLoadMoreIfNotFullPage();
        adapter.setOnLoadMoreListener(this,recyclerView);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onRefresh() {
        page = 0;
        updateData();
    }

    @Override
    public void onLoadMoreRequested() {
        updateData();
    }
}
